package com.example.administrator.readbook.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.SparseIntArray;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.readbook.R;
import com.example.administrator.readbook.adapter.ViewPagerAdapter;
import com.example.administrator.readbook.base.BaseActivity;
import com.example.administrator.readbook.navigation.MainNavigation;
import com.example.administrator.readbook.ui.fragment.BookRackFragment;
import com.example.administrator.readbook.ui.fragment.SearchFragment;
import com.example.administrator.readbook.widget.NoScrollViewPager;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements MainNavigation {
    private Intent intent;
    private String TAG = "MainActivity";

    //首页fragment
    private TextView tvTitle;
    private ImageView ivMainMenu;
    private ImageView ivMainSearch;
    private BottomNavigationViewEx mBottomNavigationViewEx;
    private NoScrollViewPager mNoScrollViewPager;
    private ViewPagerAdapter adapter;
    private List<Fragment> fragments;
    private SparseIntArray items;// used for change ViewPager selected item

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle = findViewById(R.id.main_tv_title);
        ivMainMenu = findViewById(R.id.main_iv_menu);
        ivMainSearch = findViewById(R.id.main_iv_search);
        mNoScrollViewPager = findViewById(R.id.viewpager_main);
        mBottomNavigationViewEx = findViewById(R.id.navigation_bottom_main);
        mNoScrollViewPager.setNoScroll(true);
        mNoScrollViewPager.setOffscreenPageLimit(2);
        mNoScrollViewPager.setAdapter(adapter);
        initBottomNavigation();
        mNoScrollViewPager.setCurrentItem(0);
    }

    private void initBottomNavigation() {
        mBottomNavigationViewEx.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            private int previousPosition = -1;

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//                int position = items.get(item.getItemId());
                int position = items.get(item.getItemId());
                if (previousPosition != position) {
                    previousPosition = position;
                    Log.i(TAG, "-----bnve-------- previous item:" + mBottomNavigationViewEx.getCurrentItem() + " current item:" + position + " ------------------");
                    mNoScrollViewPager.setCurrentItem(position);
                    upDataUI(position);
                }
                return true;
            }
        });

        //设置viewpager的监听 ，发生改变时改变BottomNacigation的状态
        mNoScrollViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.i(TAG, "-----ViewPager-------- previous item:" + mBottomNavigationViewEx.getCurrentItem() + " current item:" + position + " ------------------");
                mBottomNavigationViewEx.setCurrentItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.main_iv_search:
                intent = new Intent(MainActivity.this, SearchActivity.class);
                startActivity(intent);
                break;
            case R.id.main_iv_menu:
                break;
            default:
                break;
        }
    }

    @Override
    protected void initListener() {
        ivMainMenu.setOnClickListener(this);
        ivMainSearch.setOnClickListener(this);
    }

    @Override
    protected void initLoad() {

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        fragments = new ArrayList<>(2);
        items = new SparseIntArray(2);

        // add to fragments for adapter
        fragments.add(BookRackFragment.getInstance(this));
        fragments.add(SearchFragment.getInstance(this));

        // add to items for change ViewPager item
        items.put(R.id.item_menu_bookrack, 0);
        items.put(R.id.item_menu_search, 1);

        // set adapter
        adapter = new ViewPagerAdapter(fragments, getSupportFragmentManager());
    }

    @Override
    protected void initLayoutView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
    }

    private void upDataUI(int position) {
        if (position == 0) {
            tvTitle.setText(getString(R.string.action_bookrack));
            ivMainMenu.setVisibility(View.VISIBLE);
        } else if (position == 1) {
            tvTitle.setText(getString(R.string.action_search));
            ivMainMenu.setVisibility(View.GONE);
        }
    }

    @Override
    public void showBookRackFragment() {

    }

    @Override
    public void showSearchFragment() {

    }
}
